#include <string.h>
#include "trace.h"

#ifdef TRACE

#include "exception.h"

Trace::Trace()
{
	exception e;
	traceFile = fopen("trace.log", "w+t");
	if(traceFile == NULL)
	{
		throw exception("Can't open trace output file.");
	}

	indentation = 0;
}


Trace::~Trace()
{
	if(traceFile != NULL)
	{
		fclose(traceFile);
	}
}


void Trace::ReportValue(char * str, bool var)
{
	char * outputLine = new char[strlen(str) + 64];

	sprintf(outputLine, " Value of %s is \"%s\"", str, var ? "true" : "false");
	printLine(outputLine);
	
	delete[] outputLine;
}

void Trace::ReportValue(char * str, int var)
{
	char * outputLine = new char[strlen(str) + 64];

	sprintf(outputLine, " Value of %s is 0x%X", str, var);
	printLine(outputLine);
	
	delete[] outputLine;
}

void Trace::ReportValue(char * str, char var)
{
	char * outputLine = new char[strlen(str) + 64];

	sprintf(outputLine, " Value of %s is 0x%X", str, var);
	printLine(outputLine);
	
	delete[] outputLine;
}

void Trace::ReportValue(char * str, char * var)
{
	const char * varNew = var;
	ReportValue(str, varNew);
}

void Trace::ReportValue(char * str, const char * var)
{
	char * outputLine = new char[strlen(str) + strlen(var) + 64];

	sprintf(outputLine, " Value of %s is \"%s\"", str, var);
	printLine(outputLine);
	
	delete[] outputLine;
}


void Trace::ReportValue(char * str, float var)
{
	char * outputLine = new char[strlen(str) + 64];

	sprintf(outputLine, " Value of %s is %f", str, var);
	printLine(outputLine);
	
	delete[] outputLine;
}


void Trace::ReportValue(char * str, double var)
{
	char * outputLine = new char[strlen(str) + 64];

	sprintf(outputLine, " Value of %s is 0xlf", str, var);
	printLine(outputLine);
	
	delete[] outputLine;
}

void Trace::ReportValue(char * str, long var)
{
	char * outputLine = new char[strlen(str) + 64];

	sprintf(outputLine, " Value of %s is 0x%U", str, var);
	printLine(outputLine);
	
	delete[] outputLine;

}

void Trace::ReportValue(char * str, short var)
{
	char * outputLine = new char[strlen(str) + 64];

	sprintf(outputLine, " Value of %s is 0x%U", str, var);
	printLine(outputLine);
	
	delete[] outputLine;
}


void Trace::ReportLine(char * str)
{
	printLine(str);
}



void Trace::ReportEntry(char * str)
{
	char * outputLine = new char[strlen(str) + 64];

	strcpy(outputLine, "Entering function: ");
	strcat(outputLine, str);
	printLine(outputLine);
	
	indentation++;

	delete[] outputLine;
}


void Trace::ReportExit(char * str)
{
	char * outputLine = new char[strlen(str) + 64];

	indentation--;

	strcpy(outputLine, "Exiting function: ");
	strcat(outputLine, str);
	printLine(outputLine);

	delete[] outputLine;
}


void Trace::printLine(char * str)
{
	for(int i = 0; i < indentation; i++)
	{
		if(fprintf(traceFile, " ") < 0)
		{
			throw exception("Can't write trace output.");
		}
	}
    
	if(fprintf(traceFile, str) < 0)
	{
		throw exception("Can't write trace output.");
	}

	if(fprintf(traceFile, "\n") < 0)
	{
		throw exception("Can't write trace output.");
	}

	fflush(traceFile);
}


#endif

